//
//  MainVC.swift
//  ProjectApp
//
//  Created by jack on 2021/1/5.
//

import UIKit

class MainVC: BaseTabBarController {
    private var tabbarItemArray = [UITabBarItem]()
    private var navArray: [BaseNavController] = [BaseNavController]()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        var imageArray: [String] = [String]()
        var selectedImageArray: [String] = [String]()
        var vcArray: [BaseVC] = [BaseVC]()
        var titleArray: [String] = [String]()
        
        let homeVC = HomeVC()
        let mineVC = MineVC()
        let vcs = [homeVC, mineVC]
        let images = ["tab_home", "tab_mine"];
        let selectedImages = ["tab_select_home", "tab_select_mine"];
        let titles = getTitles()
        
        vcArray += vcs
        imageArray += images
        selectedImageArray += selectedImages
        titleArray += titles
        
        for (index, vc) in vcArray.enumerated() {
            let title = titleArray[index]
            let imageName = imageArray[index]
            let selectedImageName = selectedImageArray[index]
            let image = UIImage(named: imageName)
            let selectedImage = UIImage(named: selectedImageName)
            let item = UITabBarItem(title: title, image: image, selectedImage: selectedImage)
            self.tabbarItemArray.append(item)
            vc.tabBarItem = item
            let nav = BaseNavController(rootViewController: vc)
//            nav.isNavigationBarHidden = true
            navArray.append(nav)
        }
        self.viewControllers = navArray
        self.tabBar.tintColor = getTabSelectedColor()
        
        let _ = NotificationCenter.default.rx.notification(CPLANGUAGE_CHANGE_NOTIFICATION).subscribe { (not) in
             self.refreshUI()
         }
    }
    
    func refreshUI() -> Void {
        let titles = getTitles()
        for (i, title) in titles.enumerated() {
            let item = self.tabbarItemArray[i];
            item.title = title
        }
    }
    
    func getTitles() -> [String] {
        let keys = ["tab_home", "tab_mine"]
        var titles = [String]()
        for key in keys {
            let title = key.localizedString(table: nil)
            titles.append(title)
        }
        return titles
    }
}
